<template>
  <a-modal :open="open" :width="820" title="出院登记" centered :ok-button-props="{ disabled: !isCompleted }" @ok="onOk" @cancel="onCancel">
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="出院时间" class="w-1/2" v-bind="validateInfos.endtime">
        <a-date-picker v-model:value="formState.endtime" show-time value-format="YYYY-MM-DD HH:mm" format="YYYY-MM-DD HH:mm" />
      </a-form-item>
      <a-form-item label="离院方式" class="w-1/2" v-bind="validateInfos.dscg_way">
        <SelectItem v-model:value="formState.dscg_way" type="dscg_way" />
      </a-form-item>
      <a-form-item label="病种" class="w-full">
        <a-input-group compact class="w-full">
          <SelectTable
            v-model:value="formState.outDiseTypeName"
            type="医保病种"
            placeholder="病种名称"
            allow-clear
            class="!w-2/3"
            :row-disabled="(row) => row.status === 1"
            @select=";(formState.outDiseTypeName = $event?.diseTypeName || ''), (formState.outDiseTypeCode = $event?.diseTypeCode || '')"
          />
          <a-form-item-rest>
            <a-input v-model:value="formState.outDiseTypeCode" class="!w-1/3" :disabled="true" placeholder="病种代码" />
          </a-form-item-rest>
        </a-input-group>
      </a-form-item>
      <a-form-item label="手术操作" class="w-full">
        <a-input-group compact class="w-full">
          <SelectTable
            v-model:value="formState.outOprIcd9Name"
            type="手术icd9"
            placeholder="手术名称"
            class="!w-2/3"
            @select=";(formState.outOprIcd9Name = $event.icd9Name), (formState.outOprIcd9Code = $event.icd9Code)"
          />
          <a-form-item-rest>
            <a-input v-model:value="formState.outOprIcd9Code" class="!w-1/3" :disabled="true" placeholder="手术代码" />
          </a-form-item-rest>
        </a-input-group>
      </a-form-item>
      <a-form-item label="死亡日期" class="w-1/2">
        <a-date-picker v-model:value="formState.die_date" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="生育类别" class="w-1/2">
        <SelectItem v-model:value="formState.matn_type" type="birctrl_type" />
      </a-form-item>
      <a-form-item label="计划生育手术类别" class="w-1/2" :label-col="{ style: { width: '140px' } }">
        <SelectItem v-model:value="formState.birctrl_type" type="matn_type" />
      </a-form-item>
      <a-form-item label="计划生育手术或生育时间" class="w-1/2" :label-col="{ style: { width: '180px' } }">
        <a-date-picker v-model:value="formState.birctrl_matn_date" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="计划生育服务证号" class="w-1/2" :label-col="{ style: { width: '140px' } }">
        <a-input v-model:value="formState.fpsc_no" placeholder="请输入" />
      </a-form-item>
      <div class="w-1/2 flex">
        <a-form-item label="孕周数" :label-col="{ style: { width: '60px' } }">
          <a-input v-model:value="formState.geso_val" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="胎次" :label-col="{ style: { width: '48px' } }">
          <a-input v-model:value="formState.fetts" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="胎儿数" :label-col="{ style: { width: '60px' } }">
          <a-input v-model:value="formState.fetus_cnt" placeholder="请输入" />
        </a-form-item>
      </div>
      <a-form-item label="" class="w-1/2 pl-4">
        <a-checkbox v-model:checked="formState.pret_flag">早产</a-checkbox>
        <a-checkbox v-model:checked="formState.latechb_flag">晚育</a-checkbox>
        <a-checkbox v-model:checked="formState.cop_flag">伴有并发症</a-checkbox>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  inplsh: {
    type: String,
    default: ''
  }
})
const formState = ref({
  endtime: '',
  dscg_way: ''
})
const rulesState = ref({
  endtime: [{ required: true, message: '请选择出院时间' }],
  dscg_way: [{ required: true, message: '请选择离院方式' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  await validate()
  const { code, msg } = await post(
    '/inp/zyys/doDischargeRegistration',
    {
      ...formState.value,
      inplsh: props.inplsh
    },
    { loading: true }
  )
  if (code !== 0) return message.error(msg || '操作失败')
  message.success('操作成功')
  emit('success')
  emit('update:open', false)
}
const onCancel = () => {
  resetFields()
  emit('update:open', false)
}

// 获取病种&手术操作
const getInpRecordInfo = async () => {
  const { code, data, msg } = await post('/inp/zydj/getInpRecordInfo', { inplsh: props.inplsh })
  if (code !== 0) return message.error(msg)
  formState.value.outDiseTypeName = data?.outDiseTypeName
  formState.value.outDiseTypeCode = data?.outDiseTypeCode
  formState.value.outOprIcd9Name = data?.outOprIcd9Name
  formState.value.outOprIcd9Code = data?.outOprIcd9Code
}

watch(
  () => formState.value.dise_name,
  (val) => {
    if (!val) {
      formState.value.dise_codg = null
    }
  }
)
watch(
  () => formState.value.oprt_name,
  (val) => {
    if (!val) {
      formState.value.oprt_code = null
    }
  }
)

watch(
  () => props.open,
  (val) => {
    if (val) {
      props.inplsh && getInpRecordInfo()
    }
  }
)
</script>
<style lang="less" scoped></style>
